package com.heny.demo.strategy;

import com.heny.demo.common.CommonErrorCode;
import com.heny.demo.common.CommonException;
import org.apache.commons.lang3.StringUtils;

/**
 * @author litao.cc
 * @version FileProcessorStrategyEnum.java v0.1 t2025/4/24 1:39
 * @date 2025/4/24 1:39
 * @description FileProcessorStrategyEnum
 */
public enum FileProcessorStrategyEnum {
    CSV("csv", new CsvProcessor()),
    TXT("txt", new TxtProcessor()),
    XLSX("xlsx", new XlsxProcessor());
    private final String type;

    private final FileProcessorStrategy strategy;

    FileProcessorStrategyEnum(String type, FileProcessorStrategy strategy) {
        this.type = type;
        this.strategy = strategy;
    }

    public String getType() {
        return type;
    }

    public FileProcessorStrategy getStrategy() {
        return strategy;
    }

    public static FileProcessorStrategyEnum getByType(String type) {
        if (StringUtils.isBlank(type)) {
            throw new CommonException(CommonErrorCode.SYSTEM_ERROR, "type is null");
        }
        for (FileProcessorStrategyEnum item : values()) {
            if (StringUtils.equals(type, item.getType())) {
                return item;
            }
        }
        throw new CommonException(CommonErrorCode.PARAM_VALIDATION_FAILED, "不支持的文件类型,未找到对应处理策略");
    }
}
